package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 检查组
 **/
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增
     * 1、rpc调用完成新增业务
     * 2、返回结果
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkitemIds,@RequestBody CheckGroup checkGroup){
        log.info("[检查组-新增]data:{}",checkGroup);
        checkGroupService.add(checkitemIds,checkGroup);
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true,MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteCheckGroupitemById")
    public Result delete(Integer id){
        log.info("[检查组-根据id删除]id：{}",id);
        //RPC调用处理业务
        checkGroupService.delete(id);
        return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }


    /**
     * 中间表
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.info("检查组{}",id);
        //RPC调用处理业务 //这个是在检查组点击编辑的时候会先查询一下那个，里面的检查项
        List<Integer> ids = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,ids);
    }





    /**
     * 编辑
     * @param checkGroup
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup) {
        log.info("[检查项-编辑]data:", checkGroup);
        checkGroupService.edit(checkitemIds, checkGroup);

        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[检查组-根据ID查询]id:{}", id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        log.info("[检查组-查询所有]~");
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkGroups);
    }
}
